﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Admin_PerInfo_Revise : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        string userid = Session["UserId"].ToString();
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString =
        System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
        try
        {
            conn.Open();   //连接
            string SQL = "select * from Users";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;          //设置连接为前面建立的连接
            cmd.CommandText = SQL;            //  执行命令为前面的sql语句
            cmd.CommandType = CommandType.Text;         //本语句可以不要，sql语句的数据

            SqlDataReader reader = cmd.ExecuteReader();
            //使用 SqlDataReader循环读取数据
            int r = 0;
            string loginname = "";
            string password = "";
            string name = "";
            string sex = "";
            string tel = "";
            string email = "";
            while (reader.Read())      //如果可以读（有数据），就开始读
            {
                r = r + 1;
                loginname = reader["uername"].ToString();  
                password = reader["password"].ToString(); 
                name = reader["name"].ToString();  
                sex =  reader["sex"].ToString();
                tel = reader["tel"].ToString();
                email = reader["email"].ToString();
            }
            Labelloginname.Text = loginname;
            FPass.Text = password;
            FName.Text = name;
            FSex.SelectedValue = sex;
            FPhoneNum.Text = tel;
            FMail.Text = email;
        }
        finally
        {

            conn.Close();
        }
        }
    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        string userid = Session["UserId"].ToString();
        string password = FPass.Text.Trim();
        string name = FName.Text.Trim();
        string sex = FSex.SelectedValue;
        string tel = FPhoneNum.Text.Trim();
        string email = FMail.Text.Trim();

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString =
        System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
        try
        {
            conn.Open();   //连接
            string SQL = "update Users set password=@password,name=@name,sex=@sex,tel=@tel,email=@email where (uername=@uername and flag=1)";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;          //设置连接为前面建立的连接
            cmd.CommandText = SQL;            //  执行命令为前面的sql语句
            cmd.CommandType = CommandType.Text;         //本语句可以不要，sql语句的数据

            SqlParameter puername = new SqlParameter("@uername", SqlDbType.VarChar, 10);
            puername.Value = userid;
            cmd.Parameters.Add(puername);

            SqlParameter ppassword = new SqlParameter("@password", SqlDbType.VarChar, 20);
            ppassword.Value = password;
            cmd.Parameters.Add(ppassword);

            SqlParameter pname = new SqlParameter("@name", SqlDbType.NVarChar, 10);
            pname.Value = name;
            cmd.Parameters.Add(pname);

            SqlParameter psex = new SqlParameter("@sex", SqlDbType.NVarChar, 2);
            psex.Value = sex;
            cmd.Parameters.Add(psex);

            SqlParameter ptel = new SqlParameter("@tel", SqlDbType.VarChar, 11);
            ptel.Value = tel;
            cmd.Parameters.Add(ptel);

            SqlParameter pemail = new SqlParameter("@email", SqlDbType.NVarChar, 15);
            pemail.Value = email;
            cmd.Parameters.Add(pemail);
            cmd.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
        }
    }
}
